<template>
    <el-container class="home-container">
    <!-- header -->
    <el-header class="home-header">
    <div class="home-logo">
      <img src="../assets/heima.png" alt="">
      <span>电商后台管理系统</span>
    </div>
    <el-button type="info" @click="loginout">退出</el-button>
  </el-header>
  <el-container>
    <!-- aside -->
    <el-aside class="home-aside" :width="togglecollapse ? '64px' : '200px'">
      <div class="togglebtn" @click="togglebtn">|||</div>
      <el-menu
      class="el-menu-vertical-demo"
      background-color="#333741"
      text-color="#fff"
      active-text-color="#409eff"
      :unique-opened="true"
      :collapse="togglecollapse"
      :collapse-transition="false"
      :router="true"
      :default-active="activePath"
      >
      <el-submenu :index="item.id + ''" v-for="item in menulist" :key="item.id">
        <!-- 一级菜单 -->
        <template slot="title">
          <i :class="iconObj[item.id]"></i>
          <span>{{ item.authName }}</span>
        </template>
      <!-- 二级菜单 -->
        <el-menu-item
          :index="'/' + subitem.path + ''"
          v-for="subitem in item.children"
          :key="subitem.id"
          @click="saveNavState('/' + subitem.path + '')"
          >
          <template slot="title">
            <i class="el-icon-menu"></i>
            <span>{{ subitem.authName }}</span>
          </template>
        </el-menu-item>
      </el-submenu>
    </el-menu>
    </el-aside>
    <!-- main -->
    <el-main class="home-main">
      <router-view></router-view>
    </el-main>
  </el-container>
</el-container>
</template>
<script>
export default {
  data() {
    return {
      menulist: [],
      iconObj: {
        125: 'fa fa-user-plus',
        103: 'fa fa-window-close',
        101: 'fa fa-shopping-bag',
        102: 'fa fa-reorder',
        145: 'fa fa-cube'
      },
      togglecollapse: false, // 是否折叠
      activePath: ''
    }
  },
  created() {
    this.getMentList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    loginout() {
      window.sessionStorage.clear('token')
      this.$router.push('/login')
    },
    async getMentList() { // 获取所有的菜单
      const { data: res } = await this.$http.get('menus')
      // console.log(res)
      if (res.meta.status !== 200) return this.$message.error(res.meta.status)
      this.menulist = res.data
    },
    togglebtn() { // 菜单的折叠于展开
      this.togglecollapse = !this.togglecollapse
    },
    saveNavState(activePath) { // 保存子页面链接的激活高亮状态
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>
<style lang="less" scoped>
.home-container {
  height: 100%;
}
.home-header {
  background-color: #666;
  display: flex;
  justify-content: space-between;
  color: #fff;
  font-size: 20px;
  align-items: center;
  .home-logo {
    display: flex;
    justify-content: space-between;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}
.home-aside {
  background-color: #333741;
  .togglebtn {
    color: #fff;
    font-size: 10px;
    line-height: 25px;
    text-align: center;
    background-color: #999;
    letter-spacing: 0.2em;
    cursor: pointer;
  }
  .fa {
    margin-right: 10px;
  }
  .el-menu {
    border-right: none;
  }
}
.home-main {
  background-color: #fff;
}
</style>
